package com.lenin.omena.client;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.lenin.omena.client.event.LoginEvent;
import com.lenin.omena.client.event.LoginEvent.ActionType;
import com.lenin.omena.client.event.LoginEventHandler;
import com.lenin.omena.shared.LoginResult;

public class HeaderPanel extends Composite 
	implements LoginEventHandler {

	interface MyUiBinder extends UiBinder<Widget, HeaderPanel> {
	}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);
	
	@UiField
	ListBox listBox;

	@UiField
	Anchor leftLink;
	
	@UiField
	Anchor rightLink;
	
	private DialogPopup dialogPopup;

	
	public HeaderPanel() {

		initWidget(uiBinder.createAndBindUi(this));
		
		LoginEvent.register(AppleMarket.getEventBus(), this);
		
		dialogPopup = new DialogPopup();
		
		rightLink.setVisible(false);
		
		listBox.setVisibleItemCount(1);
		listBox.addItem("Helsinki");
		listBox.addItem("Tampere");
		listBox.addItem("Turku");
		listBox.addItem("Oulu");
		listBox.addItem("Jyväskylä");

	}

	@UiHandler("leftLink")
	void handleLeftClick(ClickEvent event) {

		AppleMarket.showDialogPopup();

	}
	
	@UiHandler("rightLink")
	void handleRightClick(ClickEvent event) {
		
		AppleMarket.doLogout();
	
	}
	
	@UiHandler("listBox")
	void handleSelection(ChangeEvent event) {
		
		String city = listBox.getItemText(listBox.getSelectedIndex());
		AppleMarket.centerMapOnLocation(city);
		
	}
	
	@Override
	public void onLogin(LoginEvent event) {
		
		LoginResult result = event.getResult();
		
		if(result.getStatus() == 0) {
			
			if(event.getActionType() == ActionType.LOGIN) {
				
				leftLink.setText("Profiili");
				rightLink.setVisible(true);
				
				
			} else {
				
				leftLink.setText("Kirjaudu sisään");
				rightLink.setVisible(false);
			
			}
		
		}
	
	}

}
